package com.digitalchina.lxnsy.activity.management.message;

import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.base.BaseListActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.MsgListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.viewholder.MsgListHolder;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/26 17:24
 * 修改备注：
 * ================================================
 */
public class SearchMsgListActivity extends BaseListActivity {

    private String dialogMsg = "";

    private List<MsgListVO> voList = new ArrayList<>();

    @Override
    protected void setAdapter() {

        setDecoration(1);
        showSearch();

        setTitle("消息搜索");

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MsgListHolder(parent);
            }
        });

        onRefresh();

    }

    @Override
    protected void onClick(int position) {
        super.onClick(position);
        MsgListVO vo = (MsgListVO) adapter.getAllData().get(position);
        vo.setIslook("1");
        adapter.notifyDataSetChanged();
        goDetail(vo);
    }

    private void goDetail(MsgListVO vo) {
        switch (vo.getMessageName()){
            case "农用品采购":
                GotoUtil.gotoActivity(activity, DetailFarmProductActivity.class, "MsgListVO", vo);
                break;
            case "种子采购":
                GotoUtil.gotoActivity(activity, DetailSeedActivity.class, "MsgListVO", vo);
                break;
            case "农资出入库":
                GotoUtil.gotoActivity(activity, DetailOutInActivity.class, "MsgListVO", vo);
                break;
            case "出库销售":
                GotoUtil.gotoActivity(activity, DetailProductOutActivity.class, "MsgListVO", vo);
                break;
            case "育苗":
                GotoUtil.gotoActivity(activity, DetailGrowSeedActivity.class, "MsgListVO", vo);
                break;
            case "定植":
                GotoUtil.gotoActivity(activity, DetailPlantingActivity.class, "MsgListVO", vo);
                break;
            case "浇水施肥":
                GotoUtil.gotoActivity(activity, DetailWateringActivity.class, "MsgListVO", vo);
                break;
            case "中耕除草":
                GotoUtil.gotoActivity(activity, DetailWeedingActivity.class, "MsgListVO", vo);
                break;
            case "农药使用":
                GotoUtil.gotoActivity(activity, DetailPesticideActivity.class, "MsgListVO", vo);
                break;
            case "采收":
                GotoUtil.gotoActivity(activity, DetailHarvestActivity.class, "MsgListVO", vo);
                break;
        }
    }

    @Override
    protected void search() {
        super.search();
        keyword = etSearch.getText().toString();
        page= 1;
        dialogMsg = "正在检索";
        getMsgList();
    }

    @Override
    protected void clearSearch() {
        super.clearSearch();
        keyword = "";
        getMsgList();
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0: // 获取列表
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "messageVOList", MsgListVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        dialogMsg = "";
        getMsgList();
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            dialogMsg = "";
            getMsgList();
        }
    }

    public void getMsgList() {

        /**
         * enterpriseId	企业ID
         * type	类型（0 全部 /1 已经查看 /2 待查看）
         * userAccId	用户ID
         * pageNum	页数（第几页）
         * pageSize	每页数量
         */
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", AdminXML.getUserAccId(activity));
        params.put("type", "0");
        params.put("keyword", keyword);
        params.put("pageNum", String.valueOf(page));
        params.put("pageSize", String.valueOf(limit));

        OkHttpUtil.post(activity, URL.URL_GET_MESSAGE_LIST, dialogMsg, params, mHandler,
                MSG_S_0, MSG_F_0);
    }
}
